<template>
  <dialog-editor ref="dialog"></dialog-editor>
</template>

<script>
  import DialogEditor from '@/components/DialogEditor'
  import dialogMixins from '@/utils/commonDialogMixins'
  import {mRequest} from '@/utils/request'

  export default {
    mixins: [dialogMixins],
    components: {DialogEditor},
    data() {
      return {
        dataFormConfig: {
          id: {
            hidden: true
          },
          orderDetailId: {
            label: '归为损耗订单',
            type: 'list',
            placeholder: '请选择归为损耗订单',
            list: [],
            selectCallback: this.orderDetailIdChanged
          },
          clientId: {
            label: '客户名称',
            type: 'list',
            placeholder: '客户名称',
            list: []
          },
          productId: {
            label: '产品名称',
            type: 'list',
            placeholder: '产品名称',
            list: []
          },
          productWeight:{
            label: '产品克重'
          },
          lossNumber:{
            label: '数量',
            type: 'number',
            max: 100000000
          },
          remark: {
            label: '备注',
            inputType: 'textarea',
            placeholder: '请输入备注'
          },

        }
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.initOrderDetailList()
        this.initClientInfoList()
        this.initProductInfoList()
        this.$refs.dialog.initDialog({
          dataForm: this.dataForm,
          config: this.dataFormConfig,
          dataRule: this.getNeedDataRule('orderDetailId', 'lossNumber', 'productId'),
          initApi: 'lossApi/lossLightBottleInfo',
          saveApi: 'lossApi/saveLossLightBottleInfo',
          updateApi: 'lossApi/updateLossLightBottleInfo',
          objectName: 'lossLightBottleInfo'
        })
      },
      orderDetailIdChanged() {
        const orderDetailId = this.R.propOr(0, 'orderDetailId', this.dataForm)
        if (this.R.isNil(orderDetailId) || this.R.isEmpty(orderDetailId)) {
          return
        }
        mRequest('orderApi/orderDetailInfo', {id: orderDetailId}, ({data}) => {
          this.dataForm.productId = this.R.pathOr(0, ['orderDetail', 'productId'], data)
          this.dataForm.clientId = this.R.pathOr(0, ['orderDetail', 'clientId'], data)
          this.dataForm.productWeight = this.R.pathOr(0, ['orderDetail', 'productWeight'], data)
        })
        //TODO 剩余数量 和对应的订单
        // mRequest('boxApi/boxInfoInfo', {id: torderDetailId}, ({data}) => {
        //   this.dataFormConfig['boxNumber']['max'] = this.R.pathOr(0, ['boxInfo', 'residueBoxNoCount'], data)
        // })
      },
    }
  }
</script>
